/*
 * Thread.h
 *
 *  Created on: 2010-10-17
 *      Author: tan
 */

#ifndef THREAD_H_
#define THREAD_H_

#include "types.h"
#include <string>
#include <pthread.h>
#include "light-exception.h"

using namespace std;

namespace light {

typedef void* (*Task) (void* param);
typedef void* (*TaskSimple) ();

class Thread {

public:
	void start() throw(LightException);
	void start(void* param) throw(LightException);
	void join() throw(LightException);
	void suspend() throw(LightException);
	void terminate() throw(LightException);
	int32_t getId() throw();

	static void sleep(int64_t time) throw(LightException);
	static void yeald() throw(LightException);

private:
	Task task;
	TaskSimple simpleTask;
	int32_t id;
	string name;

private:
	pthread_t threadId;
	static void* do_default_simple_task(void* param);

public:
	Thread(Task task);
	Thread(TaskSimple task);
	virtual ~Thread();
};

}

#endif /* THREAD_H_ */
